package com.guodu.service.impl;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.guodu.dao.MaterialKucunDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.MaterialKucun;
import com.guodu.entity.MaterialkucunStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.MaterialKucunService;
import com.guodu.vo.MaterialKucunQueryVo;
import com.guodu.vo.MaterialKucunVo;

@Service("materialKucunService")
public class MaterialKucunServiceImpl implements MaterialKucunService {

	@Resource
	private MaterialKucunDao materialKucunDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Override
	public List<MaterialKucunVo> findByVo(MaterialKucunQueryVo materialKucunQueryVo,Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		List<MaterialKucun> list = materialKucunDao.findByVo(materialKucunQueryVo,units, curPage, pageSize);
		List<MaterialKucunVo> materialKucunVos = new ArrayList<MaterialKucunVo>();
		for(MaterialKucun materialKucun : list ){
			MaterialKucunVo materialKucunVo = new MaterialKucunVo(materialKucun);
			UserInfo userInfo = userInfoDao.findById(materialKucun.getCreatePeople());
			Units unitsInfo = unitsDao.findById(userInfo.getUnitsId());
			materialKucunVo.setUnitsName(unitsInfo.getUnitsName());
			materialKucunVo.setIndustry(unitsInfo.getIndustry());
			materialKucunVo.setUnitsId(unitsInfo.getUnitsId());
			if(unitsInfo.getFunitsId()!=null){
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				materialKucunVo.setFunitsName(funitsInfo.getUnitsName());
				materialKucunVo.setFunitsId(funitsInfo.getUnitsId());
			}
			materialKucunVo.setUserName(userInfo.getUserName());
			materialKucunVos.add(materialKucunVo);
		}
		return materialKucunVos;
	}

	@Override
	public int findSizeByVo(MaterialKucunQueryVo materialKucunQueryVo,Units units) {
		// TODO Auto-generated method stub
		return materialKucunDao.findSizeByVo(materialKucunQueryVo,units);
	}

	@Override
	public MaterialKucun findById(String id) {
		// TODO Auto-generated method stub
		return materialKucunDao.findById(id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		materialKucunDao.del(id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generated method stub
		materialKucunDao.delByIds(ids);
	}

	@Override
	public void save(MaterialKucun entity) {
		// TODO Auto-generated method stub
		materialKucunDao.save(entity);
	}

	@Override
	public void examine(String ids,String reason,String beforeStatus,Long afterStatus,UserInfo userInfo) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<MaterialkucunStatus> entities = new ArrayList<MaterialkucunStatus>();
		for(int i=0;i<idses.length;i++){
			MaterialkucunStatus entity = new MaterialkucunStatus();
			entity.setAfterStatus(afterStatus);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(userInfo.getUserId());
			entity.setMaterialId(idses[i]);
			entity.setReason(reason);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		materialKucunDao.changeStatus(entities);
		materialKucunDao.examine(ids,afterStatus);
	}

	@Override
	public void update(MaterialKucun entity) {
		// TODO Auto-generated method stub
		materialKucunDao.update(entity);
	}

	@Override
	public List<MaterialkucunStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return materialKucunDao.findStatus(id);
	}

	@Override
	public List<MaterialKucun> findAllByVo(
			MaterialKucunQueryVo materialKucunQueryVo, Units units) {
		// TODO Auto-generated method stub
		return materialKucunDao.findAllByVo(materialKucunQueryVo, units);
	}

	@Override
	public BigDecimal findAllMoney(MaterialKucunQueryVo materialKucunQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialKucunDao.findAllMoney(materialKucunQueryVo, units);
	}

	@Override
	public boolean examinAll(MaterialKucunQueryVo materialKucunQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialKucunDao.examinAll(materialKucunQueryVo,units);
	}

	@Override
	public boolean allBack(MaterialKucunQueryVo materialKucunQueryVo,
			Units units) {
		
		return materialKucunDao.allBack(materialKucunQueryVo,units);
	}

	@Override
	public boolean huituiAll(MaterialKucunQueryVo materialKucunQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialKucunDao.huituiAll(materialKucunQueryVo,units);
	}

	@Override
	public boolean shanchuAll(MaterialKucunQueryVo materialKucunQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialKucunDao.shanchuAll(materialKucunQueryVo,units);
	}



}
